package com.gdcp.newland;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

import com.nle.mylibrary.claimer.connector.ConnectorListener;
import com.nle.mylibrary.databus.DataBus;
import com.nle.mylibrary.databus.DataBusFactory;
import com.nle.mylibrary.device.GenericConnector;
import com.nle.mylibrary.device.listener.ConnectResultListener;

import java.util.List;
import cn.com.newland.nle_sdk.requestEntity.SignIn;
import cn.com.newland.nle_sdk.responseEntity.SensorInfo;
import cn.com.newland.nle_sdk.responseEntity.User;
import cn.com.newland.nle_sdk.responseEntity.base.BaseResponseEntity;
import cn.com.newland.nle_sdk.util.NCallBack;
import cn.com.newland.nle_sdk.util.NetWorkBusiness;
public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private TextView tem;
    private TextView temData;
    private TextView hum;
    private TextView humData;
    private US mUser=new US();
    private Handler mHandler;
    private String AccessToken="";
    private Handler han=new Handler(Looper.getMainLooper());
    private DataBus mDataBus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);




        init();
        getdata();
        mHandler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(@NonNull Message message) {
                if (message.what==1){
                    AccessToken=(String) message.obj;
                    Log.e(TAG, "handleMessage: "+AccessToken);
                }

                                mDataBus=DataBusFactory.newSocketDataBus("172.20.88.15",6005);
                                GenericConnector ge=new GenericConnector(mDataBus, new ConnectResultListener() {
                                    @Override
                                    public void onConnectResult(boolean b) {
                                        Log.e(TAG, "onConnectResult: "+b );
                                    }

                                });
                                if(message.what==2){
                                    String obj =(String) message.obj;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        try {
                                            ge.LedScreenCmd(obj, new ConnectorListener() {
                                                @Override
                                                public void onSuccess(boolean b) {
                                                    Log.e(TAG, "onSuccess: " + b);

                                                }

                                                @Override
                                                public void onFail(Exception e) {

                                                }
                                            });
                                        } catch (Exception e) {
                                            e.printStackTrace();
                                        }


                                    }

                                });}




                return false;
            }
        });

    }
    private void getdata() {
        final NetWorkBusiness mNetWorkBusiness=new NetWorkBusiness("",mUser.getUrl());
        mNetWorkBusiness.signIn(new SignIn(mUser.getRoot(), mUser.getPwd()), new NCallBack<BaseResponseEntity<User>>(getApplicationContext()) {
            @Override
            protected void onResponse(BaseResponseEntity<User> userBaseResponseEntity) {
                String accessToken = userBaseResponseEntity.getResultObj().getAccessToken();
                Message message=new Message();
                message.what=1;
                message.obj=accessToken;
                mHandler.sendMessage(message);



        new Thread(new Runnable() {
    @Override
    public void run() {

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                NetWorkBusiness netWorkBusiness=new NetWorkBusiness(accessToken,mUser.getUrl());
                netWorkBusiness.getSensors("669416", "wen,hum", new NCallBack<BaseResponseEntity<List<SensorInfo>>>(getApplicationContext()) {
                    @Override
                    protected void onResponse(BaseResponseEntity<List<SensorInfo>> asd) {

                        String tem = asd.getResultObj().get(0).getValue();
                        String hum=asd.getResultObj().get(1).getValue();
                        mUser.setData(tem);


                        humData.setText(hum);
                        temData.setText(tem);

                        Message message1=new Message();
                        message1.what=2;
                        message1.obj=tem;
                        mHandler.sendMessage(message1);





                    }
                });
                han.postDelayed(this,5*1000);

            }


        });
        try{
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}).start();
            }
        });

    }
    private void init(){
        tem=findViewById(R.id.temp);
        hum=findViewById(R.id.hum);
        temData=findViewById(R.id.tempData);
        humData=findViewById(R.id.humData);
    }
}